package com.cloud.hai_kang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloud.exception.CustomException;
import com.cloud.hai_kang.vo.HikVisionVo;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 海康工具类（参数文档地址）
 * https://open.hikvision.com/docs/docId?productId=5c67f20bb254d61550c2f63e
 * &version=%2F8c59548d449e4e1ea43397dfa22248f4&curNodeId=2e8fa43bcbd348d68f1d6334877ae8ce
 *
 * @author hhb, yzj
 */
public class HikUtils {

    // API网关的后端服务上下文为: /artemis
    private static final String ARTEMIS_PATH = "/artemis";

    // 平台IP地址+端口
    private String host;

    private String key;

    private String secret;

    public HikUtils(String host, String key, String secret) {
        this.host = host;
        this.key = key;
        this.secret = secret;
    }

    /**
     * 获取海康威视数据
     *
     * @param api 请求接口地址
     * @param map 接口请求参数
     * @return 结果集
     */
    private String getResult(String api, Map<String, Object> map) {
        ArtemisConfig config = new ArtemisConfig();
        // 代理API网关Nginx服务器ip端口
        config.setHost(host);
        // 密钥AppKey
        config.setAppKey(key);
        // 密钥AppSecret
        config.setAppSecret(secret);
        // 请求路径
        final String camsApi = ARTEMIS_PATH + api;
        String body = JSON.toJSON(map).toString();
        Map<String, String> path = new HashMap<>(2) {
            {
                put("https://", camsApi);
            }
        };
        String result = null;
        try {
            result = ArtemisHttpUtil.doPostStringArtemis(config, path, body,
                    null, null, "application/json", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取监控点资源列表
     *
     * @return
     */
    public List<HikVisionVo> getMonitorList() {
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", "1");
        map.put("pageSize", "1000");
        String result = getResult("/api/resource/v1/cameras", map);
        JSONObject jsonData = JSONObject.parseObject(result);
        if ("0".equals(jsonData.getString("code"))) {
            JSONObject data = JSONObject.parseObject(jsonData.getString("data"));
            return JSONArray.parseArray(data.getString("list"), HikVisionVo.class);
        } else {
            throw new CustomException(jsonData.getString("msg"));
        }
    }

    /**
     * 获取监控点预览取流URL
     *
     * @param code
     * @return
     */
    public String getMonitorFlow(String code) {
        Map<String, Object> map = new HashMap<>();
        map.put("cameraIndexCode", code);
        map.put("protocol", "hls");
        map.put("streamType", 1); // 获取子码流地址
        String result = getResult("/api/video/v1/cameras/previewURLs", map);
        JSONObject jsonData = JSONObject.parseObject(result);
        if ("0".equals(jsonData.getString("code"))) {
            Map<String, String> dataMap = JSONArray.parseObject(jsonData.getString("data"), Map.class);
            return dataMap.get("url");
        } else {
            throw new CustomException(jsonData.getString("msg"));
        }
    }

    public String getRegionInfoByCode(String regionCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("regionIndexCode", regionCode);
        String result = getResult("/api/resource/v1/region/indexCode/regionInfo", map);
        JSONObject jsonData = JSONObject.parseObject(result);
        String code = jsonData.getString("code");
        if (code.equals("0")) {
            JSONObject data = jsonData.getJSONObject("data");
            return data.getString("name");
        }
        return null;
    }

}
